package zuoti.com;

public class Text {
    //先搞全局变量
    boolean[][] flg;
    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};
    int m,n;
    int sum;
    public int getMaximumGold(int[][] grid) {
        //初始化
        m = grid.length;
        n =grid[0].length;
        flg = new boolean[m][n];
        for(int i = 0;i < m; i++) {
            for(int j = 0; j < n;j++) {
                if(grid[i][j] != 0) {
                    flg[i][j] = true;
                    dfs(grid,i,j,grid[i][j]);
                    //恢复
                    flg[i][j] = false;
                }
            }
        }
        return sum;
    }

    public void dfs(int[][] grid,int i, int j,int path) {
        sum = Math.max(sum,path);
        for(int k = 0; k < 4; k++) {
            int x = i + dx[k];
            int y = j + dy[k];
            if(x >= 0 && x < m && y >=0 && y < n && !flg[x][y] && grid[x][y] != 0) {
                flg[x][y] = true;
                dfs(grid,x,y,path+grid[x][y]);
                flg[x][y] = false;
            }
        }
    }
    public static void main(String[] args) {
        //力扣 1219题   黄金矿工
        //考察算法  深搜  dfs  遍历  递归

    }
}
